capture log close
clear
capture program drop _all
set more off
timer clear 1
timer on 1
set seed 123

**************** 
***  PATHS   ***  
****************
*BCCR: 1 if at Central Bank
global BCCR=1
if $BCCR == 1 {
	global path "Y:/Projects/MNC_Suppliers/Suppliers_Alfaro-Urena_Manelici_Vasquez"
}

if $BCCR == 0 {
	global path "~/Documents/GitHub/Suppliers_Alfaro-Urena_Manelici_Vasquez"
}
cd $path 

*log file
local logfile "${path0}/log_files/8-appendix_e"
set linesize 255
log using `logfile', replace
set linesize 255

/*
RESULTS IN APPENDIX E OF THE PAPER
*/

********************** 
***  INPUT FILES   ***  
**********************
global revec ""raw_data/revec_group.dta""
global events_data ""processed_data/events_data.dta""
global transactions ""raw_data/trans_clean_corresp.dta""
global MNCs ""processed_data/MNC_sample.dta""
global analysis_data ""processed_data/analysis_data.dta""

global D151 ""raw_data/trans_clean_corresp.dta""
global IOres ""raw_data/IO_matrix/IOres_demanda.dta""
global IOres_noimports ""raw_data/IO_matrix/IOres_noimports.dta""

*full sample vs restricted
global sample1 "if past!=1"
global sample2 "if event!=."
global cluster1 "province#sector_g"
global cluster2 "province#event"
estimates clear

********************************************************************************
* main program: outline of the full code
********************************************************************************
prog main

*Table E1: Blalock and Gertler 2008
	blalock_sector_level
	
*Tables E2-E7 are produced in "3-main_results.do" together with Figure 3
*Figures E1-E2 are produced in "3-main_results.do" together with Figure 3

*Table E8: inferred \sigma from DLW (2012)
	param_dlw
	
*Table E9 is produced in "3-main_results.do" together with Table 5

*Table E10: similar compliance in third party reporting
	compliance_transactions
	
*Table E11: no change in workers hired from informality/unemployment 
	compliance_workers
	
end

********************************************************************************
*Table E1: Blalock and Gertler 2008
********************************************************************************
{

********************** 
***  OUTPUT FILES  ***  
**********************
global shares_s2s ""processed_data/shares_s2s.dta""
global shares_f2s ""processed_data/shares_f2s.dta""
global shares ""processed_data/shares.dta""
global partials ""processed_data/partials.dta""
global back_forw_IO ""processed_data/back_forw_IO.dta""
global share2sector ""processed_data/share2sector.dta""
global linkages ""processed_data/linkages.dta""

********************************************************************************
* blalock_gertler outline of the full code
********************************************************************************
prog blalock_sector_level

* shares of transactions per buyer or seller sector 
	quiet share_trans

* shares from CR IO matrix 
	IO_shares 
	
* IO matrix using transactions database 
	IO_transactions 
	
* Blalock and Gertler 2008
	blalock_gertler_2008
end 


********************************************************************************
* shares of transactions per buyer or seller sector 
********************************************************************************
prog share_trans  

*revec firms 
use $revec , clear
keep ID firm_AE 
merge 1:1 ID using $MNCs
drop forei*
gen foreign_list=(_m==3)
keep ID firm_AE foreign_list
replace firm_AE="" if firm_AE==" "
save temp/rev.dta , replace

drop foreign_lis
rename ID seller 
rename firm_AE s_AE
save temp/sellers.dta, replace

rename seller buyer 
rename s_AE b_AE
save temp/buyers.dta, replace

*** firm to sector 
use $D151, clear
drop if pot==1
drop pot
merge m:1 buyer using temp/buyers.dta
keep if _m==3 | _m==1
rename _m merge_b

merge m:1 seller using temp/sellers.dta
keep if _m==3 | _m==1
rename _m merge_s

bys seller year: egen s_trans=total(trans)
bys buyer year: egen b_trans=total(trans)

keep if merge_b==3 | merge_s==3
drop merge*
compress
save temp/temp_trans.dta , replace 

** buyers
preserve
bys buyer year s_AE: egen purch_sector=total(trans)
gen sigma_f2s= purch_sector/b_trans
collapse sigma_f2s, by(buyer year s_AE)
rename buyer ID
rename s_AE partner_AE
tempfile buyers
save `buyers', replace
restore

** sellers
bys seller year b_AE: egen sales_sector=total(trans)
gen alpha_f2s= sales_sector/s_trans
collapse alpha_f2s s_trans, by(seller year b_AE)
rename seller ID
rename b_AE partner_AE
merge 1:1 ID year partner_AE using `buyers'
drop _m 
replace sigma=0 if sigma==.
replace alpha=0 if alpha==.
merge m:1 ID using temp/rev.dta
drop if _m==2
drop _m

save $shares_f2s, replace
	
*** SECTOR TO SECTOR
use temp/temp_trans.dta , clear
collapse trans , by(b_AE s_AE)
bys b_AE : egen sector_pur_IO=total(trans)
bys s_AE : egen sector_sales_IO=total(trans)
rename trans sector2sector_trans

gen sigma= sector2sector_trans/sector_pur
replace sigma=0 if sigma==.
gen alpha= sector2sector_trans/sector_sales_IO
replace alpha=0 if alpha==.
keep b_AE s_AE alpha sigma sector2sector_trans
rename alpha alpha_s2s
rename sigma sigma_s2s
save $shares_s2s, replace

	
end

********************************************************************************
* shares from CR IO matrix 
********************************************************************************
prog IO_shares 

***  MIP
use $IOres_noimports, clear
capture drop utilizacion_tot
reshape long AE, i(cprod) j(cdem)
rename AE prod
tostring cdem, replace
replace cdem=substr(cdem,-3,.)

***  % by column and row   
quietly bys cdem: egen double total_bycolumn=total(prod)
quietly bys cprod: egen double total_byrow=total(prod)

gen sigma = 0
replace sigma=prod/total_bycolumn if total_bycolumn>0
gen alpha = 0
replace alpha=prod/total_byrow if total_byrow>0
save $shares, replace

clear
*** DOMESTIC
use $analysis_data, clear
collapse year, by(ID)
keep ID 
tempfile IDs
save `IDs', replace
use ${revec}, clear
preserve 
use $MNCs, clear
keep ID 
tempfile mncs
save `mncs', replace
restore
merge 1:1 ID using `mncs'
drop forei*
gen foreign_list=(_m==3)
drop _m
merge 1:1 ID using `IDs'
keep if _m==3 | foreign_lis==1
drop _m

forvalue i=2008/2017{
replace ingresosir_`i'= ${tc_`i'}*exports_`i' if ingresosir_`i'<= ${tc_`i'}*exports_`i' & ingresosir_`i'!=. & exports_`i'!=.
}

forvalue i=2008/2017{
gen forshare_`i'=forshare
quietly bys firm_AE: egen double prodtotjt_`i'=total(ingresosir_`i')
quietly bys firm_AE: egen double yminusx_`i'=total(ingresosir_`i'-${tc_`i'}*exports_`i')
gen horizontalfirm_`i'=forshare_`i'*ingresosir_`i'
quietly bys firm_AE: egen double horizontal_`i'=total(horizontalfirm_`i')
replace horizontal_`i'=horizontal_`i'/prodtotjt_`i'

gen forwardfirm_`i'=forshare_`i'*(ingresosir_`i'-${tc_`i'}*exports_`i')
quietly bys firm_AE: egen double forwardparc_`i'=total(forwardfirm_`i')
replace forwardparc_`i'=forwardparc_`i'/yminusx_`i'
}
collapse (mean)  horizontal* forwardparc*, by (firm_AE)

keep firm_AE horizontal* forwardparc*

tostring firm_AE, replace
drop if firm_AE=="A0100" |  firm_AE=="A0111" | firm_AE=="A0113" | firm_AE=="A0119" |   firm_AE=="A0122" | firm_AE=="A0126" | firm_AE=="A0127" | firm_AE=="A0141" | firm_AE=="A0146" | firm_AE=="A1010" | firm_AE=="A1061" |  firm_AE=="A1079" |firm_AE=="A4922" | firm_AE=="A9810" | firm_AE=="A9820" |  firm_AE=="A9900"
replace firm_AE=substr(firm_AE,-3,.)
rename firm_AE cdem
drop if cdem==""
save $partials, replace

use $shares, clear
merge m:1 cdem using $partials
 
***  Backward 			  
forvalue i=2008/2017{
gen backwardpart_`i'=alpha*horizontal_`i'
gen backwardexclude_`i'=backwardpart_`i' if cdem==cprod
quietly bys cprod: egen double backward_`i'=total(backwardpart_`i')
replace backwardexclude_`i'=0 if backwardexclude_`i'==.
replace backward_`i'=backward_`i'-backwardexclude_`i'
}
tempfile partialresults
save `partialresults', replace
 
***  Forward 			
forvalue i=2008/2017{
gen forwardpart_`i'=sigma*forwardparc_`i'
gen forwardexclude_`i'=forwardpart_`i' if cdem==cprod
quietly bys cdem: egen double forward_`i'=total(forwardpart_`i')
replace forwardexclude_`i'=0 if forwardexclude_`i'==.
replace forward_`i'=forward_`i'-forwardexclude_`i'
}

collapse (mean) forward*, by (cdem)
rename cdem firm_AE

tempfile forward
save `forward', replace

use `partialresults', clear
collapse (mean) backward*, by (cprod)
rename cprod firm_AE
tempfile backward
save `backward', replace

erase `partialresults'

use $partials, clear
keep cdem horizontal*
rename cdem firm_AE
merge 1:1 firm_AE using `forward'
drop _merge
merge 1:1 firm_AE using `backward'

erase `backward'
erase `forward'

keep firm_AE horizontal* forward* backward* 

replace firm_AE="AE"+firm_AE
keep firm_AE horizontal_* backward_* forward_*
reshape long horizontal_ backward_ forward_ , i(firm_AE) j(year)
drop if firm_AE=="AE"
replace back=0 if back==.
replace forw=0 if forw==.
replace hori=0 if hori==.
save $back_forw_IO, replace

end

********************************************************************************
* IO matrix using transactions database  
********************************************************************************
prog IO_transactions
*** DOMESTIC
use $analysis_data, clear
keep ID year
tempfile IDs
save `IDs', replace
*** MNCs
use $MNCs, clear
keep ID
tempfile fore_rest
save `fore_rest'
*** revec
use $revec , clear
drop for*
merge 1:1 ID using `fore_rest'
gen foreign_list=(_m==3)
drop _m
gen forshare=100*foreign_list
*rename forsh foreign_list
*** buyers and sellers 
preserve
keep ID foreign_l firm_AE
tempfile IDrevec
save `IDrevec', replace
rename ID buyer
rename firm_AE b_ae_
rename foreign_l b_foreign
tempfile buyers
save `buyers', replace
rename buyer seller
rename b_foreign s_foreign
rename b_ae_ s_ae_
tempfile sellers
save `sellers', replace
restore
forvalue i=2008/2017{
replace ingresosir_`i'= ${tc_`i'}*exports_`i' if ingresosir_`i'<= ${tc_`i'}*exports_`i' & ingresosir_`i'!=. & exports_`i'!=.
replace exports_`i'= ${tc_`i'}*exports_`i'
}
* SELLERS 
keep ID ingresosir_* exports_* foreign_list* ae_* trabaj_*
local resh_var "ingresosir_ exports_  ae_ trabaj_"
reshape long `resh_var', i(ID) j(year)
compress 
drop if year<2008 | year>2017
gen estado=(trabaj_!=. & ingresosir!=.)
drop if (trabaj_!=. | ingresosir!=.)==0
tostring estado, replace
bys ID: egen entry=min(year)
bys ID: egen exit=max(year)
replace exit=. if exit>=2017
keep if year>=entry
drop if year>=exit
keep if estado=="1"
rename ae AE
*** HORIZONTAL: FOREIGN PRESENCE USING SALES BY SECTOR (AE)
replace export=0 if export==. & ingresosir!=.
gen foreign_sales= ingresosir*foreign
*Generate total sales share of foreign (horizontal_ingresosir)
bys AE year: egen sector_sales_f =total(foreign_sales)
bys AE year: egen sector_sales_total =total(ingresosir)
gen horizontal_ingresosir= 100*sector_sales_f/sector_sales_total
replace horizontal_ingresosir=0 if horizontal_ingresosir==.
drop sector_sales_f sector_sales_total
*Generate domestic sales share of foreign (horizontal_forward)
gen domestic=ingresosir-export
gen domestic_foreign_sales= domestic*foreign_l
bys AE year: egen sector_sales_f =total(domestic_foreign_sales)
bys AE year: egen sector_sales_total =total(domestic)
gen horizontal_forward= 100*sector_sales_f/sector_sales_total
replace horizontal_forward=0 if horizontal_forward==.
preserve
bys AE year: gen dup=cond(_N==0,0,_n)
drop if dup>1
drop dup
drop if AE==""
drop if AE==" "
keep AE year horizontal*
tempfile temp_
save `temp_', replace
restore 
keep ID AE year 
merge m:1 year AE using `temp_'
drop _m
tempfile IDs
save `IDs', replace
keep AE year hori*
bys AE year: gen dup=cond(_N==1,0,_n)
drop if dup>1
drop dup
tempfile hori
save `hori', replace
*** FIRM TO FIRM
quiet{
use $D151, clear
drop if potent==1
drop potent
merge m:1 buyer using `buyers'
rename _m b_m
replace b_fore=0 if b_fore==.
merge m:1 seller using `sellers'
*** TRANSACTIONS FOR WHICH WE KNOW SECTORS OR NOT
bys seller year: egen s_tot=total(trans)
gen trans_with_sector=(b_ae_!="" & b_ae_!=" ")*trans_
bys seller year: egen trans_info=total(trans_with_sector)
drop trans_with_sector trans_info s_tot
replace s_fore=0 if s_fore==.
keep if b_m==3 &  _m==3
drop _m b_m
drop if b_ae_==" " | b_ae_==""
drop if s_ae_==" " | s_ae_==""
*total transactions involving buyer or seller
bys seller year: egen s_total_trans=total(trans_)
bys buyer year: egen b_total_trans=total(trans_)
*Amount of each sale and purchase that corresponds foreign ownership
gen sales_foreign= trans_*b_foreign
gen purchases_foreign= trans_*s_foreign
*total for foreign sales/purchases by FIRM
bys seller year: egen tot_sales_foreign=total(sales_foreign)
bys buyer year: egen tot_purchases_foreign=total(purchases_foreign)
*** Firm to firm: BACKWARD
gen backward_f2f= 100*tot_sales_foreign/s_total_trans if tot_sales_foreign!=. &  s_total_trans!=.
replace backward_f2f=0 if backward_f2f==.
preserve
keep seller year backward_f2f 
bys seller year: gen dup=cond(_N==1,0,_n)
drop if dup>1
drop dup
rename seller ID
merge 1:1 ID year using `IDs'
drop _m
save `IDs', replace
restore
*** Firm to firm: FORWARD
gen forward_f2f= 100*tot_purchases_foreign/b_total_trans if tot_purchases_foreign!=. &  b_total_trans!=.
replace forward_f2f=0 if forward_f2f==.
preserve
keep buyer year forward_f2f
bys buyer year: gen dup=cond(_N==1,0,_n)
drop if dup>1
drop dup
rename buyer ID
merge 1:1 ID year using `IDs'
drop _m
save `IDs', replace
restore
}
*** FIRM TO SECTOR
quiet{
*** FIRM TO SECTOR: BACKWARDS
preserve
*Sectors defined by the buyer
rename b_ae_ AE
*Merge with the previous file that has  horizontal_ingresosir 
merge m:1 AE year using `hori'
drop _m
*Total sales of each seller to each sector
bys AE year seller: egen sales_sector=total(trans_)
gen share_sales2sector= sales_sector/s_total_trans
bys seller year AE: gen dup=cond(_N==0,0,_n)
drop if dup>1
drop dup
tempfile share2sector
save `share2sector', replace
keep seller year AE share_sales horizontal_ingresosir
*rename horizontal_ingresosir horizontal
drop if AE==""
drop if AE==" "
egen sector=group(AE)
drop if sector==.
replace share_sales2sector=0 if share_sales2sector==.
*Calculate the backward multiplying the horizontal shares times the share of sales
*from a firm to each sector
gen backward_f2s= share_sales2sector*horizontal
collapse (sum) backward_f2s , by(seller year)
rename seller ID
*rename horizontal horizontal_ingresosir 
keep ID year backward_f2s   
tempfile temp_
save `temp_', replace
restore

* Shares of each firm sold to each sector
preserve
use  `share2sector', clear
keep seller year s_ae AE share_sales 
rename seller ID
rename s_ae firm_AE
rename AE buyer_sector
compress
drop if firm_AE==""
drop if firm_AE==" "
drop if ID==""
drop if ID==" "
save $share2sector, replace
restore
preserve
use `IDs', clear
merge m:1 ID year using `temp_'
replace backward_f2s=0 if _m==1
drop _m
save `IDs', replace
restore
*** FIRM TO SECTOR: FORWARD
preserve
*Sectors defined by the seller
rename s_ae_ AE
*Merge with the previous file that has horizontal_forward
merge m:1 AE year using `hori'
drop _m
*Total purchases of each buyer to each sector
bys AE year buyer: egen purchases_sector=sum(trans_)
bys buyer year: egen purchases=total(trans_)
gen share_sales2sector= purchases_sector/purchases
bys buyer year AE: gen dup=cond(_N==0,0,_n)
drop if dup>1
drop dup
keep buyer year AE share_sales horizontal*
drop if AE==""
drop if AE==" "
egen sector=group(AE)
drop if sector==.
summ sector
local n_sector=`r(max)'
keep horizontal_forward buyer share year sector
gen forward_f2s= share_sales2sector*horizontal_f
collapse (sum) forward_f2s , by(buyer year)
rename buyer ID
keep ID year forward_f2s
tempfile temp_
save `temp_', replace
restore
preserve
use `IDs', clear
merge m:1 ID year using `temp_'
replace forward_f2s=0 if _m==1
drop _m
save `IDs', replace

order ID  year AE  hori* back* forw*, first
drop horizontal_fo

***  MERGING WITH REVEC   
drop if ID==""
merge m:1 ID using `IDrevec'
keep if _m==3
drop if AE==""
drop if AE==" "
drop if ID==""
drop if ID==" "
capture drop hori*
drop _m
merge m:1 AE year using `hori'
keep ID year AE hori* back* forw* forei*  
local vari " backward_f2f forward_f2f horizontal_ingresosir" 
*drop  horizontal_ingresosir
foreach vari_ of local vari{
replace `vari_'=0 if `vari_'==.
}
drop if year<2008
summ hori* back* forw*
}
***  SAVING  
drop if ID==""
save $linkages, replace
restore

*** SECTOR TO SECTOR
quiet{
collapse trans_, by(year b_ae s_ae)
drop if b_ae=="" | s_ae==""
drop if b_ae==" " | s_ae==" "

rename trans prod
rename b_ae cdem 
rename s_ae cprod

tostring cdem, replace
replace cdem=substr(cdem,-3,.)
replace cprod=substr(cprod,-3,.)

***  % by column and row  
quietly bys cdem year: egen double total_bycolumn=total(prod)
quietly bys cprod year: egen double total_byrow=total(prod)
gen sigma = 0
replace sigma=prod/total_bycolumn if total_bycolumn>0
gen alpha = 0
replace alpha=prod/total_byrow if total_byrow>0
keep cprod cdem alpha sigma year
reshape wide alpha sigma , i(cprod cdem) j(year)

tempfile outputfile1
save `outputfile1', replace

clear
use $revec, clear
forvalue i=2008/2017{
replace ingresosir_`i'= ${tc_`i'}*exports_`i' if ingresosir_`i'<= ${tc_`i'}*exports_`i' & ingresosir_`i'!=. & exports_`i'!=.
}
forvalue i=2008/2017{
gen forshare_`i'=forshare
quietly bys firm_AE: egen double prodtotjt_`i'=sum(ingresosir_`i')
quietly bys firm_AE: egen double yminusx_`i'=sum(ingresosir_`i'-${tc_`i'}*exports_`i')
*replace yminusx_`i'=0 if yminusx_`i'<0
gen horizontalfirm_`i'=forshare_`i'*ingresosir_`i'
quietly bys firm_AE: egen double horizontal_`i'=sum(horizontalfirm_`i')
replace horizontal_`i'=horizontal_`i'/prodtotjt_`i'

gen forwardfirm_`i'=forshare_`i'*(ingresosir_`i'-${tc_`i'}*exports_`i')
quietly bys firm_AE: egen double forwardparc_`i'=sum(forwardfirm_`i')
replace forwardparc_`i'=forwardparc_`i'/yminusx_`i'
}
collapse (mean)  horizontal* forwardparc*, by (firm_AE)

keep firm_AE horizontal* forwardparc*

tostring firm_AE, replace
drop if firm_AE=="A0100" |  firm_AE=="A0111" | firm_AE=="A0113" | firm_AE=="A0119" |   firm_AE=="A0122" | firm_AE=="A0126" | firm_AE=="A0127" | firm_AE=="A0141" | firm_AE=="A0146" | firm_AE=="A1010" | firm_AE=="A1061" |  firm_AE=="A1079" |firm_AE=="A4922" | firm_AE=="A9810" | firm_AE=="A9820" |  firm_AE=="A9900"
replace firm_AE=substr(firm_AE,-3,.)
rename firm_AE cdem
drop if cdem==""

tempfile outputfile2
save `outputfile2', replace
 
use `outputfile1', clear
merge m:1 cdem using `outputfile2'

***  Backward 			   
forvalue i=2008/2017{
gen backwardpart_`i'=alpha`i'*horizontal_`i'
gen backwardexclude_`i'=backwardpart_`i' if cdem==cprod
quietly bys cprod: egen double backward_`i'=sum(backwardpart_`i')
replace backwardexclude_`i'=0 if backwardexclude_`i'==.
replace backward_`i'=backward_`i'-backwardexclude_`i'
}
tempfile partialresults
save `partialresults', replace

***  Forward 			  
forvalue i=2008/2017{
gen forwardpart_`i'=sigma`i'*forwardparc_`i'
gen forwardexclude_`i'=forwardpart_`i' if cdem==cprod
quietly bys cdem: egen double forward_`i'=sum(forwardpart_`i')
replace forwardexclude_`i'=0 if forwardexclude_`i'==.
replace forward_`i'=forward_`i'-forwardexclude_`i'
}

collapse (mean) forward*, by (cdem)
rename cdem firm_AE

tempfile forward
save `forward', replace

use `partialresults', clear
collapse (mean) backward*, by (cprod)
rename cprod firm_AE
tempfile backward
save `backward', replace

erase `partialresults'

use `outputfile2', clear
keep cdem horizontal*
rename cdem firm_AE
merge 1:1 firm_AE using `forward'
drop _merge
merge 1:1 firm_AE using `backward'

erase `backward'
erase `forward'

keep firm_AE horizontal* forward* backward* 

replace firm_AE="AE"+firm_AE
keep firm_AE horizontal_* backward_* forward_*
reshape long horizontal_ backward_ forward_ , i(firm_AE) j(year)
drop if firm_AE=="AE"
replace back=0 if back==. | back<0
replace forw=0 if forw==. | forw<0
replace hori=0 if hori==. | hori<0

drop hori
rename forward_ forward_trans
rename backward_ backward_trans
rename firm_AE AE
tempfile IO_d151
save `IO_d151'
}

***  SAVING   
merge 1:m year AE using $linkages
drop if year<2008
keep if _m==3
drop _m
rename AE firm_AE

save $linkages, replace

end

********************************************************************************
* Blalock and Gertler 2008
********************************************************************************
prog blalock_gertler_2008

use $analysis_data, clear
merge 1:1 ID year using $linkages
keep if _m==3
global vari "forward_trans forward_f2f forward_f2s backward_trans backward_f2f backward_f2s horizontal_ingresosir "
foreach v of global vari {
	replace `v'=`v'/100
}
global controls "k l m c.l#c.l c.k#c.k c.m#c.m c.k#c.l c.l#c.m c.k#c.m"

***
*** LAGS
***
xtset IDs year, year
foreach v of local vari{
replace `v'=0 if `v'==.
gen lag_`v'=l.`v'
}

capture gen back=.
capture gen forw=.
capture gen lback=.
capture gen lforw=.
capture gen hor=.
capture gen lhor=.
label variable back "Backward"
label variable forw "Forward"
label variable lback "Backward Lagged"
label variable lforw "Forward Lagged"
label variable hor "Horizontal"
label variable lhor "Horizontal Lagged"
eststo clear
global REG "reghdfe y $controls hor back forw  , absorb(year#province IDs) vce(robust)" 

estimates clear
*sector2sector level
replace back=backward_trans
replace forw=forward_trans
replace hor=horizontal_ing
run_reg_blalock
estimates store m_1

*firm2firm level 
replace back=backward_f2f
replace forw=forward_f2f
run_reg_blalock
estimates store m_2


* Prepare estimates for -estout-
	estfe m_*, labels(IDs "Firm FE" year#sector4 "Year-4DSect FE" year#province "Year-Prov FE")
	return list		
esttab m_* using ${path0}/results/5-appendix_e/Supplementary_Table_E1.tex, indicate(`r(indicate_fe)')  star(* 0.10 ** 0.05 *** 0.01) varwidth(23)  cells("b(fmt(3) star )" "se(par fmt(3))" ) ///
	stats(r2_a mean_dep sd_dep n_obs n_fe n_firms , fmt(a2) labels("Adjusted R$^2$" "Mean Dep. Var. (level)" "SD Dep. Var. (level)" "\# Observations" "\# Fixed Effects" "\# Firms" ))     ///
	mlabel( "Sector-to-sector"  "Firm-to-firm") alignment(l) keep(back hor forw) order(back hor) ///
 replace noconstant label nodepvar collabels(none)   compress	type

end
eststo clear

********************************************************************************
*run regressiona and stores relevant info
********************************************************************************
prog run_reg_blalock
capture drop resid
capture drop dep

*regression 
$REG resid(resid) 

* add number of observations 
scalar n_obs=e(N)
estadd scalar n_obs

* add distinct number of firms
distinct IDs if resid!=.
scalar n_firms=r(ndistinct)
estadd scalar n_firms 

* add mean and sd of dependent variable 
global dep_var "`e(depvar)'"
gen dep=.
if "$quant" == ""{
*in US dollars
replace dep=exp( ${dep_var} )/500
}
if "$quant" == "1" {
replace dep=exp( $dep_var )
}
if "$quant" == "1" & "$logs"=="0" {
replace dep=$dep_var
}
global quant ""
global logs ""
quietly summ dep if resid!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep

*add number of fixed effects 
scalar n_fe=e(df_a_initial)
estadd scalar n_fe

capture drop resid
capture drop dep
end



}


********************************************************************************
*Table E8: inferred \sigma from DLW (2012)
********************************************************************************
prog param_dlw
display "Inferring returns and sigma from DLW (2012)"
quiet{

use $analysis_data, clear

*define an extra sector to contain all firms
preserve
replace sector_g=100
tempfile sector_all
save `sector_all', replace
restore

append using `sector_all'
eststo clear

*    TABLE: Cobb-Douglas DLW

*drop sectors with less than 500 obs in the regression
drop if sector_g==18 | sector_g==15
* REGRESSION BY SECTORS
eststo clear
levelsof sector_g, local(levels)
display "`levels'" 
local lbe: value label sector_g
display "`lbe'"

foreach l of local levels{
local f`l': label `lbe' `l'
}

local models ""
foreach l of local levels{

preserve 
keep if sector_g==`l'

***    DLW GMM BASED ON DLW CODE
clear mata
gen temp_l=l
gen temp_k=k
gen temp_m=m
drop l* k* m*
rename temp_l l
rename temp_k k
rename temp_m m 

* higher order terms on inputs
local M=3
local N=3
forvalues i=1/`M' {
gen l`i'=l^(`i')
gen m`i'=m^(`i')
gen k`i'=k^(`i')
*interaction terms
forvalues j=1/`N' {
gen l`i'm`j'=l^(`i')*m^(`j')
gen l`i'k`j'=l^(`i')*k^(`j')
gen k`i'm`j'=k^(`i')*m^(`j')
}
}
gen lkm=l*k*m
gen l2k2m2=l2*k2*m2
gen l3k3m3=l3*k3*m3
* generate interaction terms of all (l,m,k) 
*with exportdummy:call all terms e*(.) below
*------------------------------------------------------------------------------*
* INITIAL VALUES
* CD
quietly reghdfe va m l k, absorb(IDs year)
scalar blols=_b[l]
scalar bkols=_b[k]
scalar bmols=_b[m]

*------FIRST STAGE  -----------------------------------------------------------*
*xi: reg y l* m* k* i.year
quietly reghdfe va m* l* k*, absorb(year IDs) res(epsilon)
predict phi
label var phi "phi_it 
label var epsilon "measurement error first stage
xtset IDs year, yearly
gen phi_lag=L.phi
*------------------------------------------------------------------------------*
gen l_lag=L.l
gen k_lag=L.k
gen m_lag=L.m
gen l_lag2=l_lag^2
gen k_lag2=k_lag^2
gen m_lag2=m_lag^2
gen l_lagk_lag=l_lag*k_lag
gen l_lagm_lag=l_lag*m_lag
gen k_lagm_lag=k_lag*m_lag
gen lm=l*m
gen lk=l*k
gen km=k*m
gen l_lagk=l_lag*k
gen l_lagm=l_lag*m
*---COMPUTE CORRECTED SHARES---------------------------------------------------*
gen y_c=va-epsilon
gen va_c=exp(y_c)
gen alpha_l=salarios/va_c/1000
*------------------------------------------------------------------------------*
sort IDs year
gen const=1
drop if y==.
drop if l_lag==.
drop if k==.
drop if m==.
drop if phi==.
drop if phi_lag==.

*MATA GMM routine 
	quietly do ${path0}/all_codes/Z-GMM_mata.do

bootstrap r(beta_l) r(beta_k) r(mu) r(returns_) r(sigma) ///
 r(obs_) , reps(100): dlw
gen omega_dlw=phi-_b[_bs_1]*l-_b[_bs_2]*k-_b[_bs_3]*m
gen mu=_b[_bs_1]/alpha_l

scalar obs_`l'=_b[_bs_6]
scalar lc_`l'=_b[_bs_4]
scalar lc_se_`l'=_se[_bs_4]
*pvalue for constant returns
scalar p_`l'=2*normal(-abs((_b[_bs_4]-1)/_se[_bs_4]))

quiet{
esttab, se nostar nocons 
}
* SAVING RESULTS IN MATRIX
matrix C_`l' = r(coefs)
* taking out the number of obs
scalar r = rowsof(C_`l')
scalar c = colsof(C_`l')
matrix C_`l'[`=r',`=c']=.
matrix coleq C_`l'="sector`l'"
mat rownames C_`l'=Labor Capital Markups Returns Sigma Obs  
mat colnames C_`l'=b se
if `l'==1{
mat C=C_1
}
else{
mat C=C,C_`l'
}
restore
}
eststo clear

local models0 : coleq C
display "`models0'"
local models : list uniq models0
display "`models'"
local i 0

***
*** CREATING THE TRANSPOSED TABLE
***
local rnames "Labor Capital Markups Returns Sigma Obs"
display "`rnames'"
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
	capture matrix drop obs
	capture matrix drop r
	capture matrix drop p
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = "`model'"
            matrix b = nullmat(b), tmp
            matrix obs = nullmat(obs), tmp		
			matrix r = nullmat(r), tmp	
			matrix p = nullmat(r), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
		
        }
    }
    ereturn post b
	quietly estadd matrix se
    quietly estadd matrix obs
    quietly estadd matrix p		
    eststo `name'
}
}
esttab  using ${path0}/results/5-appendix_e/Supplementary_Table_E8.tex, ///
se b(%9.2f) nostar varwidth(40) mtitles("Labor" "Capital" "$\mu$" "Returns to scale" "$\sigma$" "Number of obs") noobs ///
label coeflabels(sector1 "Agriculture, forestry and fishing" /// 
sector2 "Manufacturing" ///
sector3 "Electricity and Gas" ///
sector4 "Water Supply, Sewerage and Waste Manag." ///
sector5 "Construction" ///
sector6 "Wholesale and Retail Trade" ///
sector7 "Transportation and Storage" ///
sector8 "Accommodation and Food Services" ///
sector9 "Information and Communication" ///
sector11 "Professional, Scientific and Technical" ///
sector12 "Administrative and Support Service" ///
sector14 "Human Health and Social Work" ///
sector16 "Other Services" ///
sector100 "All sectors (Pooled)") nogaps compress type replace  
end

********************************************************************************
*Table E10: similar compliance in third party reporting
********************************************************************************
prog compliance_transactions
display "compliance in third party reporting"
quiet{

*loading data	
use $analysis_data , clear

*dummies to show in tables  	
ds D_m4-D_4
global dummies `r(varlist)'

estimates clear
capture drop red
* COLUMN 1: NEVER MATCHED + EVENTUALLY MATCHED, VAR: DIFF SELLER
reghdfe seller_diff  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g) resid(red)
distinct IDs if red!=.
scalar n_firms=r(ndistinct)
capture drop dep
gen dep=seller_diff
quietly summ dep if red!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
scalar n_obs=e(N)
scalar n_fe=e(df_a_initial)
display `=n_fe' 
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_1
* COLUMN 2: NEVER MATCHED + EVENTUALLY MATCHED, VAR: DIFF BUYER
reghdfe buyer_diff  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g) 
capture drop dep
gen dep=buyer_diff
quietly summ dep if red!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_2
* COLUMN 3: NEVER MATCHED + EVENTUALLY MATCHED, VAR: MISREPORTING AS SELLER
reghdfe sh_misreported_s  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g) 
capture drop dep
gen dep=sh_misreported_s
quietly summ dep if red!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_3
capture drop red

if $BCCR == 1{
* COLUMN 4: EVENTUALLY MATCHED, VAR: DIFF SELLER
reghdfe seller_diff  D_*  if event!=. , absorb(year#sector4#province IDs) vce(cluster province#event) resid(red)
distinct IDs if red!=.
scalar n_firms=r(ndistinct)
capture drop dep
gen dep=seller_diff
quietly summ dep if red!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
scalar n_obs=e(N)
scalar n_fe=e(df_a_initial)
display `=n_fe' 
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "NO"
estimates store m_4
* COLUMN 5: EVENTUALLY MATCHED, VAR: DIFF BUYER
reghdfe buyer_diff  D_*  if event!=. , absorb(year#sector4#province IDs) vce(cluster province#event) 
distinct IDs if red!=.
scalar n_firms=r(ndistinct)
capture drop dep
gen dep=buyer_diff
quietly summ dep if red!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "NO"
estimates store m_5
* COLUMN 6: EVENTUALLY MATCHED, VAR: MISREPORTING AS SELLER
reghdfe sh_misreported_s  D_*  if event!=. , absorb(year#sector4#province IDs) vce(cluster province#event)
distinct IDs if red!=.
scalar n_firms=r(ndistinct)
capture drop dep
gen dep=sh_misreported_s
quietly summ dep if red!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "NO"
estimates store m_6
* Prepare estimates for -estout-
	estfe m_*, labels(IDs "Firm FE" year#sector4#province "Year-4DSect-Prov FE")
	return list	
}	
}
esttab m_* using ${path0}/results/5-appendix_e/Supplementary_Table_E10.tex, ///
 indicate(`r(indicate_fe)' Never_Matched)  star(* 0.10 ** 0.05 *** 0.01) ///
 varwidth(23) keep(${dummies}) ///
 cells("b(fmt(3) star )" "se(par fmt(3) )" ) ///
 stats(r2_a mean_dep sd_dep n_obs n_fe n_firms , fmt(a2) labels("Adjusted R$^2$" "Mean Dep. Var. (level)" "SD Dep. Var. (level)" "\# Observations" "\# Fixed Effects" "\# Firms" )) ///
 mlabel(Seller-Diff Buyer-Diff Mis-Seller Seller-Diff Buyer-Diff Mis-Seller) ///
 alignment(c) coeflabels(none) replace noconstant label nodepvar compress type	
end

********************************************************************************
*Table E11: no change in workers hired from informality/unemployment 
********************************************************************************
{
prog compliance_workers

quiet{
estimates clear
global cond1 ""
global cond2 "drop if foreign_worker==1"

forvalue h=1/2{

*first, we need to bring the employer-employee data (same code as in managers exercise)
capture confirm file temp/ee_full.dta
if _rc!=0 {
do ${path0}/all_codes/Z-MEED.do	
}

*using output data drom Z-MEED
use temp/ee_full, clear

*year of birth
gen yob=substr(f_nac,-4,.)
destring yob , replace force
*keep people older than 30 years old in 2008
keep if yob<=1978
gen age=year-yob
drop if age>60
*employer is highest paying firm for each person on a given year
sort num_identi year sal, stable
by num_identi year: gen pos=_N-_n+1
keep if pos==1
drop pos
gen foreign_worker=(codpais!=188)

*worker that comes from unemployment or informality
xtset num_id year, y
gen nowhere=(l.year==.)
gen nowhere_foreigner=nowhere*foreign_worker

*share of new workers coming from nowhere
gen new_hire=(ID!=l.ID)
gen new_foreigner=(ID!=l.ID)*foreign_worker
drop if year<2008

gen tot_workers=1

*applying condition of excluding or not foreigners
${cond`h'}

collapse (sum) nowhere* new_* foreign_worker tot_workers,by(ID year)
gen sh_nowhere_foreigners_among_new=nowhere_foreigner/new_hire
gen sh_nowhere_among_new=nowhere/new_hire
gen sh_foreign_worker=foreign_worker/tot_workers
keep ID year sh_* *nowhere* *foreign_worker
tostring ID, replace format(%24.0g)

*merging with analysis_data
merge 1:1 ID year using $analysis_data
drop if _m==1
drop _m
replace sh_nowhere_among_new=0 if sh_nowhere_among_new==.
replace sh_foreign_worker=0 if sh_foreign_worker==.

*regressions
global reg_var "sh_nowhere_among_new"
*baseline
global condition1 ""
global n_conditions=1
global type_ "compl_`h'_work"  
reg_ee
}
}
***
*** workers compliance table
***
#delimit ;
esttab  compl_1_work_1_1 compl_2_work_1_1 compl_1_work_2_1 compl_2_work_2_1 using 
${path0}/results/5-appendix_e/Supplementary_Table_E11.tex, 
indicate(`r(indicate_fe)' Never_Matched) 
 star(* 0.10 ** 0.05 *** 0.01) varwidth(23) keep(${dummies}) 
 cells("b(fmt(3) label($\theta$) star )" "se(par fmt(3) label((SE)) )" ) 
stats(r2_a mean_dep sd_dep n_obs n_fe n_firms , fmt(a2) labels("Adjusted R$^2$" 
"Mean Dep. Var. (level)" "SD Dep. Var. (level)" "\# Observations" 
"\# Fixed Effects" "\# Firms" )) mlabel("No" "Yes" 
"No" "Yes") alignment(c) 
replace noconstant coeflabels(none) label nodepvar compress type ; 
#delimit cr

end

******
* REGRESSIONS   
******
prog reg_ee

ds D_m4-D_4
global dummies `r(varlist)'

forvalue j=1/2{
forvalue i=1/$n_conditions {
capture drop red
* REGRESSION
global REG "reghdfe $reg_var D_* ${sample`j'} ${condition`i'} , absorb(year#sector4#province IDs) vce(cluster ${cluster`j'})"
run_reg
if `j'==1{
estadd local Never_Matched "YES"
addfe Never_Matched
}
estimates store ${type_}_`j'_`i'
capture drop red
}
}

end


}
********************************************************************************
*run regression and stores relevant info
********************************************************************************
prog run_reg
capture drop resid
capture drop dep

*regression 
$REG resid(resid) 

* add number of observations 
scalar n_obs=e(N)
estadd scalar n_obs

* add distinct number of firms
distinct IDs if resid!=.
scalar n_firms=r(ndistinct)
estadd scalar n_firms 

* add mean and sd of dependent variable 
global dep_var "`e(depvar)'"
if "$quant" == ""{
*in US dollars
gen dep=exp( $dep_var )/500
}
if "$quant" == "1" {
gen dep=exp( $dep_var )
}
global quant ""
quietly summ dep if resid!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep

*add number of fixed effects 
scalar n_fe=e(df_a_initial)
estadd scalar n_fe

capture drop resid
capture drop dep
end

********************************************************************************
*PROGRAM TO ADD FIXED EFFECTS INDICATOR TO LATEX TABLES           
********************************************************************************
program addfe
    if `"`0'"'=="" local 0 _fe
    tempname b V
    mat `b' = 0
    mat coln `b' = `0'
    mat `b' = e(b), `b'
    mat `V' = e(V)
    mat `V' = (`V', J(rowsof(`V'), 1, 0)) \ (J(1, colsof(`V'), 0), 0)
    erepost b=`b' V=`V', rename
end

*program that performs DLW
program dlw, rclass
preserve 
sort IDs year
mata DLW()
return scalar beta_l=abs(beta_dlw[1,2])
return scalar beta_k=abs(beta_dlw[1,3])

gen mu_= beta_dlw[1,2]/alpha_l
quietly summ mu_, det
local per1=`r(p1)'
local per99=`r(p99)'
keep if mu_<`per99' & mu_>`per1'
quietly summ mu_, det
return scalar mu=`r(mean)'
return scalar obs_=_N
**
** sigma
**
return scalar sigma= `r(mean)'/(`r(mean)'-1)
**
** returns of scale 
**
return scalar returns_= beta_dlw[1,2]+beta_dlw[1,3]
end

********************************* 
***  AVERAGE EXCHANGE RATE    ***  
*********************************
* 1 Set of each year: average compra-venta
global tc_2005=477.76578
global tc_2006=511.29055
global tc_2007=516.59063
global tc_2008=526.35683
global tc_2009=573.35611
global tc_2010=525.68364
global tc_2011=505.69
global tc_2012=502.8868
global tc_2013=499.75405
global tc_2014=538.36123
global tc_2015=534.55408
global tc_2016=544.76658
global tc_2017=567.55509

******************************************************************************** 
***  EXECUTE THE PROGRAM main   
******************************************************************************** 
main

******************************************************************************** 
***  TIMER
********************************************************************************
timer off 1
*time in seconds
timer list 1
*time in minutes
local time_ =round(`r(t1)'/60)
display "the code takes `time_' minutes in total"

log close
translate `logfile'.smcl `logfile'.txt , replace linesize(255)
erase `logfile'.smcl
